import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { BROWSER_SEARCH_ITEMS, SETTING_STORAGE_NAME, SYSTEM_SEARCH_ITEMS } from '@renderer/constants'
import type { IBrowserSearch, ISystemSearch } from '@renderer/types'

interface State {
  shortcutKey: string;
  openAtLogin: boolean; // 开机启动
  managerShortcutKey: string;
  systemSearchItems: ISystemSearch[];
  browserSearchItems: IBrowserSearch[];
  defaultSearchType: string;
  setShortcutKey: (str: string) => void;
  setOpenAtLogin: (bool: boolean) => void;
  setManagerShortcutKey: (str: string) => void;
  updateSystemSearchItem: (itemId: string, data: Partial<Omit<ISystemSearch, 'id'>>) => void;
  addBrowserSearchItem: (item: IBrowserSearch) => void;
  removeBrowserSearchItem: (itemId: string) => void;
  updateBrowserSearchItem: (itemId: string, data: Partial<Omit<IBrowserSearch, 'id'>>) => void;
  setDefaultSearchType: (type: string) => void;
}

export const useSettingStore = create(
  persist<State>(
    (set) => ({
      shortcutKey: 'Alt + Space',
      managerShortcutKey: 'Control + .',
      openAtLogin: false,
      systemSearchItems: SYSTEM_SEARCH_ITEMS,
      browserSearchItems: BROWSER_SEARCH_ITEMS,
      defaultSearchType: 'snippets',
      setShortcutKey: (shortcutKey: string) => {
        set({
          shortcutKey,
        })
      },
      setOpenAtLogin: (openAtLogin: boolean) => {
        set({
          openAtLogin,
        })
      },
      setManagerShortcutKey: (managerShortcutKey: string) => {
        set({
          managerShortcutKey,
        })
      },
      updateSystemSearchItem: (itemId: string, data: Partial<Omit<ISystemSearch, 'id'>>) => {
        set(state => ({
          systemSearchItems:
            state.systemSearchItems.map(item => {
              if (item.id === itemId) {
                return {
                  ...item,
                  ...data,
                }
              }
              return { ...item }
            }),
        }))
      },
      addBrowserSearchItem: (item: IBrowserSearch) => {
        set(state => ({
          browserSearchItems: [...state.browserSearchItems, item],
        }))
      },
      removeBrowserSearchItem: (itemId: string) => {
        set(state => ({
          browserSearchItems:
            state.browserSearchItems.filter(i => i.id !== itemId),
        }))
      },
      updateBrowserSearchItem: (itemId: string, data: Partial<Omit<IBrowserSearch, 'id'>>) => {
        set(state => ({
          browserSearchItems:
            state.browserSearchItems.map(item => {
              if (item.id === itemId) {
                return {
                  ...item,
                  ...data,
                }
              }
              return { ...item }
            }),
        }))
      },
      setDefaultSearchType: (defaultSearchType: string) => {
        set({
          defaultSearchType,
        })
      },
    }),
    {
      name: SETTING_STORAGE_NAME,
      storage: createJSONStorage(() => localStorage),
    },
  ),
)
